package com.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.entity.Employee;
import com.reggie.entity.Orders;
import com.reggie.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author liangzai
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private IOrdersService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     *
     *  订单列表
     * @param page     当前查询页码
     * @param pageSize 每页展示记录数
      * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number) {
        log.info("page = {},pageSize = {},number = {}", page, pageSize, number);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //执行查询
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }




    /**
     *
     *  订单详情
     * @param page     当前查询页码
     * @param pageSize 每页展示记录数
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        Long currentId = BaseContext.getCurrentId();
         //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(Orders::getUserId, currentId);
        //执行查询
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

}
